package com.rinvay.asdownloader.core;

import com.rinvay.asdownloader.core.network.NetworkManager;
import com.rinvay.asdownloader.persistent.DownloadTaskDAO;
import com.rinvay.network.filedownload.FileDownloadManager;

public class Core
{
    private static Core instance;
    
    private NetworkManager networkMgr;
    
    private FileDownloadManager downloadMgr;
    
    private Core () {
        NetworkManager.createInstance(this);
        
        downloadMgr = new FileDownloadManager();
    }
    
    public static void createInstance() {
        if(instance == null) {
            instance = new Core();
        }
    }
    
    public static Core getInstance() {
        return instance;
    }
    
    public static void terminate() {
        if(instance != null) {
            instance.stopCore();
        }
        instance = null;
    }
    
    public void startCore() {
        networkMgr = NetworkManager.getInstance();
        networkMgr.start();
        
        downloadMgr.getResourceList().addAll(
                DownloadTaskDAO.getInstance().queryAllDownloadTask());
    }
    
    public void stopCore() {
        networkMgr.stop();
    }
    
    public FileDownloadManager getFileDownloadManager() {
        return downloadMgr;
    }

    public NetworkManager getNetWorkManager() {
        return networkMgr;
    }
}
